package com.framework.dubbo.producer.file2html;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.framework.dubbo.producer.config.Constants;

import java.io.File;
import java.io.InputStream;

/**
 * 抽象预览工具
 * @author xianggj
 * @Date 2021/6/8 14:12
 **/
public abstract class AbstractFilePreviewTools {

    /**
     * 本地文件
     */
    private static final String LOCAL_DIR = "local";

    protected String staticPath;

    protected String staticUrl;

    protected String oldFileName;

    public AbstractFilePreviewTools setStaticPath(String staticPath) {
        this.staticPath = staticPath;
        return this;
    }

    public AbstractFilePreviewTools setStaticUrl(String staticUrl) {
        this.staticUrl = staticUrl;
        return this;
    }

    public AbstractFilePreviewTools setOldFileName(String oldFileName) {
        this.oldFileName = oldFileName;
        return this;
    }

    public  String getLocalDir() {
        String id = IdUtil.simpleUUID();
        return staticPath + LOCAL_DIR + Constants.SEPARATOR + id;
    }

    /**
     * 流输入到本地
     * @param in
     * @return
     */
    public String writeToLocal(InputStream in, String ext){
        String id = IdUtil.simpleUUID();
        FileUtil.writeFromStream(in, staticPath + LOCAL_DIR + Constants.SEPARATOR + id + Constants.DOT + ext);
        return staticUrl + LOCAL_DIR + Constants.SEPARATOR + id + Constants.DOT + ext;
    }
    /**
     * 路径查看
     * @param path
     * @return
     */
    public abstract String convert2Html(String path) ;

    /**
     * 流查看
     * @param in
     * @param ext
     * @return
     */
    public abstract String convert2Html(InputStream in, String ext);


    /**
     * 将流文件转换成文本
     * @param in
     * @param ext
     * @return
     */
    public abstract String convert2Str(InputStream in, String ext);

    /**
     * 文件查看
     * @param file
     * @param ext
     * @return
     */
    public String convert2Html(File file, String ext){
        InputStream in = FileUtil.getInputStream(file);
        return convert2Html(in, ext);
    }

}
